layui.config({
    base: 'assets/layuiadmin/' //静态资源所在路径
}).extend({
    index: 'lib/index' //主入口模块
}).use(['index', 'form'], function(){
    var $ = layui.$,
        form = layui.form,
        element = layui.element;

    var id = $('#id').val();
    var data = {'id': id};
    post('version/toEdit', data, false,function (data) {
        var sysVersion = data.sysVersion;
        $('input[name="type"]').eq(sysVersion.type).attr('checked', 'checked');
        $('#version').val(sysVersion.version);
        $('#code').val(sysVersion.code);
        $('#content').text(sysVersion.content);

        if(sysVersion.url != undefined && sysVersion.url != ''){
            $('#iconDel').show();
            $('#file-upload-span').text(sysVersion.url.substring(sysVersion.url.lastIndexOf("/")+1));
            $('#url').val(sysVersion.url);
        }
        if(sysVersion.must == 1){
            $('input[name="must"]').eq(1).attr('checked', 'checked');
        }
        form.render('radio');
    });

    //加载上传控件
    upload("file-upload-div", function (up, file, info) {
        var option = up.getOption();
        var host = option.url;
        var dir = option.multipart_params.key;

        var url = host + '/' + dir;
        $('#file-upload-span').text(url.substring(url.lastIndexOf("/")+1));
        $('#url').val(dir);
        $('#iconDel').show();
    }, function (percent) {
        element.progress('progress', percent+"%")
    }, false);

    //删除图片
    $('#iconDel').on('click', function (event) {
        $('#file-upload-span').text('');
        $('#url').val('');
        $('#iconDel').hide();

        event.stopPropagation();    //阻止点击事件向上传播
    });

    //校验
    form.verify({
        'file-verify': function (value, item) {
            var type = $('input[name="type"]:checked').val();
            var url = $('#url').val();
            if(type == 0 && (url == '' || url == undefined)){
                return '请上传安装包';
            }
        }
    });
});